////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.data.sqlite {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import plantool.data.QueryErrorResult;
	import plantool.data.QueryResult;
	import plantool.data.Table;
	import plantool.core.CoreDispatcher;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	[Event(name="complete", type="flash.events.Event")]
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class CreateTableHelper extends CoreDispatcher {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _currentField:uint			= 0;
		private var _table:Table				= null;
		private var _onCompleteHandler:Function	= null;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function CreateTableHelper(onCompleteHandler:Function) {
			super();
			
			_onCompleteHandler = onCompleteHandler;
		}
		
		public override function dispose():void {
			//clean up the oncomplete handler
			_onCompleteHandler 	= null;
			_table 				= null;
			_currentField		= 0;
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function createField():void {
			if (_currentField < _table.fields.length) {
				//execute a select query to check if the field already exists
				var query:String = "SELECT `" + _table.fields[_currentField].name + "` FROM `" + _table.name + "`";
				
				//execute the query
				databaseManager.query(query, fieldExists, fieldNotExists);
			} else {
				//we're done adding all fields
				_onCompleteHandler();
				
				//dispose this
				dispose();
			}
		}
		
		private function fieldExists(result:QueryResult):void {
			//increate current field counter
			_currentField++;
			
			//create next field
			createField();
		}
		
		private function fieldNotExists(result:QueryErrorResult):void {
			//create the query to add the fields
			var query:String = "ALTER TABLE `" + _table.name + "` ADD COLUMN `" + _table.fields[_currentField].name + "` " + databaseManager.getFieldName(_table.fields[_currentField].type);
			
			//execute the query
			databaseManager.query(query, onComplete_addField, onError_addField);
		}
		
		private function onComplete_addField(result:QueryResult):void {
			//increate current field counter
			_currentField++;
			
			//create next field
			createField();
		}
		
		private function onComplete_createTable(result:QueryResult):void {
			//create the fields for this table
			createField();
		}
		
		private function onError_addField(result:QueryResult):void {
			//increate current field counter
			_currentField++;
			
			//create next field
			createField();
		}
		
		private function onError_query(result:QueryErrorResult):void {
			trace("Error executing query:", result.query);
			trace(" - Message:", result.message);
		}
		
		public function create(table:Table):void {
			//store table
			_table	= table;
			
			//check if settings table exists
			databaseManager.query("CREATE TABLE IF NOT EXISTS `" + _table.name + "`(`id` INTEGER PRIMARY KEY)", onComplete_createTable, onError_query);
		}
	}
}